package com.xiangxiangjd.analyse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiangxiangjd.analyse.bean.RepairMethod;
import com.xiangxiangjd.analyse.bean.TreeDto;
import com.xiangxiangjd.analyse.controller.entity.ApiCode;
import com.xiangxiangjd.analyse.controller.helper.$;
import com.xiangxiangjd.analyse.utils.TreeToolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xiangxiangjd.analyse.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiangxiangjd.analyse.service.RepairStructureService;
import com.xiangxiangjd.analyse.bean.RepairStructure;
import com.xiangxiangjd.analyse.controller.model.ApiResultModel;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2021-03-08
 */
@Controller
@RequestMapping("/analyse/repairStructure")
public class RepairStructureController extends BaseController {


    /**
     * 主页面
     */
    @RequestMapping("admin/main.html")
    public String main() {
        return "RepairStructure/RepairStructureMain";
    }

    /**
     * 添加页面跳转
     *
     * @return 返回页面
     */
    @RequestMapping(value = "admin/addRepairStructureHtml")
    public String addRepairStructureHtml() {
        return "RepairStructure/RepairStructureAdd";
    }

    /**
     * 编辑页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/editRepairStructureHtml")
    public String editRepairStructureHtml(String id, Model model) {

        RepairStructure repairStructure = repairStructureService.selectRepairStructureByIdRepairStructure(id);

        model.addAttribute("repairStructure", repairStructure);

        /**
         * 判断父级id 是否为0
         */
        String parentName = repairStructure.getParentId().equals("0") ? "无上级" : repairStructureService.getById(repairStructure.getParentId()).getStructureName();

        model.addAttribute("parentName", parentName);

        return "RepairStructure/RepairStructureEdit";
    }

    /**
     * 查看页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/showRepairStructureHtml")
    public String showRepairStructureHtml(String id, Model model) {
        model.addAttribute("repairStructure", repairStructureService.selectRepairStructureByIdRepairStructure(id));
        return "RepairStructure/RepairStructureShow";
    }


    /**
     * 分页查询RepairStructure
     *
     * @param page            页码
     * @param limit           查几条
     * @param repairStructure 前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "admin/findRepairStructureByPage")
    @ResponseBody
    public ResultPage findRepairStructureByPage(Integer page, Integer limit, RepairStructure repairStructure) {
        return repairStructureService.findRepairStructureByPage(page, limit, repairStructure);
    }

    /**
     * 添加RepairStructure
     *
     * @param repairStructure 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/addRepairStructure")
    @ResponseBody
    public ApiResultModel saveRepairStructure(RepairStructure repairStructure) {
        RepairStructure data = repairStructureService.addRepairStructure(repairStructure);

        if(data == null ){$.error();}


        return $.success();
    }

    /**
     * 编辑RepairStructure
     *
     * @param repairStructure 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/editRepairStructure")
    @ResponseBody
    public ApiResultModel editRepairStructure(RepairStructure repairStructure) {
        return repairStructureService.editRepairStructure(repairStructure);
    }


    /**
     * 删除RepairStructure
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/delRepairStructure", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultModel delRepairStructure(@RequestParam("datas[]") ArrayList ids) {


        return repairStructureService.delRepairStructures(ids);
    }


    /**
     * 获取树形结构
     *
     * @return
     */
    @RequestMapping(value = "admin/getTreeStructure", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultModel delRepairStructure() {

        //    List<RepairStructure> treeStructure = repairStructureService.getTreeStructure();
        ArrayList<TreeDto> rootList = new ArrayList<>();//根节点
        ArrayList<TreeDto> bodyList = new ArrayList<>();//子节点

        List<RepairStructure> industryList = repairStructureService.list();


        for (int i = 0; i < industryList.size(); i++) {
            TreeDto treeDto = new TreeDto(
                    industryList.get(i).getStructureId().toString(),
                    industryList.get(i).getStructureName(),
                    industryList.get(i).getParentId().toString(),
                    "true", null
            );
            bodyList.add(treeDto);
        }


        TreeDto treeDto = new TreeDto("0", "结构信息", "null", "true", null);

        rootList.add(treeDto);

        TreeToolUtils utils = new TreeToolUtils(rootList, bodyList);


        List<TreeDto> result = utils.getTree();

        return $.success(ApiCode.success, ApiCode.success_msg, result);


    }


    /**
     * 更具父级id 获取子集对象
     *
     * @param parentId
     * @return
     */
    @RequestMapping(value = "/admin/getASubset", method = RequestMethod.POST)
    @ResponseBody
    public ApiResultModel delRepairMethod(String parentId) {

        //

        List<RepairStructure> list = repairStructureService.list(new LambdaQueryWrapper<RepairStructure>().eq(RepairStructure::getParentId, parentId));

        return $.success(list);
    }


}
